﻿using AnesSystem.ViewModels;
using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AnesSystem.Udp;
using System.Threading;
using Common;

namespace AnesSystem.Controls
{
    /// <summary>
    /// LoginCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class LoginCtrl : UserControl
    {
        #region Variables

        private LoginCtrlViewModel _model;

        #endregion

        #region Constructor

        public LoginCtrl()
        {
            InitializeComponent();
            _model = LoginCtrlViewModel.Instance;
            RoomsComboboxCtrlViewModel.Instance.SelectedRoom = DataManager.Instance.CurRoom;
            this.DataContext = _model;
            Loaded += LoginCtrl_Loaded;
        }

       

        #endregion

        #region Properties

        #endregion

        #region Event

        public event EventHandler LoginClicked;

        #endregion

        #region Private Methods


        #endregion

        #region Public/Protected Methods

        public void SetDataContext(object data)
        {
            this.DataContext = data;
        }

        #endregion

        #region EventHandlers

        private void LoginCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            txtPwd._txtpasswd.Focus();
        }

        private void _btnLogin_Click(object sender, RoutedEventArgs e)
        {
            DataManager.Instance.CurRoom = RoomsComboboxCtrlViewModel.Instance.SelectedRoom;
            ClientAndServerHelper.Instance.WrtiteRoomToLocal(DataManager.Instance.CurRoom.Name);
            ClientAndServerHelper.Instance.WrtiteLoginNameToLocal(txtName.Text);

            if (!string.IsNullOrEmpty(DataManager.Instance.CurRoom.Name))
            {
                DataManager.Instance.Login.LoginName = txtName.Text;
                DataManager.Instance.Login.Pwd = txtPwd._txtpasswd.Password;
                _model.LoginExecute();
                if (_model.Login_status)
                    LoginClicked?.Invoke(sender, e);
                else
                    MessageBoxCommon.ShowDialog("用户名或密码错误!", MessageBoxCommon.OK);
            }
            else
                MessageBoxCommon.ShowDialog("请选择手术室!", MessageBoxCommon.OK);
        }


        #endregion
    }
}
